package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Reply;
import com.hl.sfzy.service.ReplyService;
import com.hl.sfzy.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/reply")
public class ReplyController {
	@Autowired
	private ReplyService replyService;

	@PostMapping("/add")
	public R add(@RequestBody Reply reply) {
		int result = replyService.add(reply);
		if(result != 0) {
			return R.ok(result);
		}
		return R.fail("添加失败");
	}

	@PutMapping ("/edit")
	public R edit(@RequestBody Reply reply) {
		int result = replyService.edit(reply);
		if(result != 0) {
			return R.ok(result);
		}
		return R.fail("修改失败");
	}

	@DeleteMapping("/remove")
	public R remove(Integer replyId) {
		int result = replyService.remove(replyId);
		if(result != 0) {
			return R.ok(result);
		}
		return R.fail("删除失败");
	}

	@DeleteMapping("/removeBatch")
	public R removeBatch(Integer[] replyIds) {
		int result = replyService.removeBatch(replyIds);
		if(result != 0) {
			return R.ok(result);
		}
		return R.fail("批量删除失败");
	}

	@GetMapping("/find")
	public R find() {
		return R.ok(replyService.find());
	}

	@GetMapping("/findById")
	public R findById(Integer replyId) {
		return R.ok(replyService.findById(replyId));
	}

	@GetMapping("/findByPage")
	private R findByPage(
			@RequestParam(required = false ,defaultValue = "1")int pageNum,
			@RequestParam(required = false,defaultValue = "5") int pageSize, Reply reply) {
		PageInfo<Reply> replys = replyService.findByPage(pageNum, pageSize, reply);
		return R.ok(replys);
	}


}
